import { AppCTX } from "../../types/koaExtend";
import { createSpokesmanApiMWRouter } from "../../middleware/spokesman-api/brandSpokesmanChecker";
import {
  checkActivityOfSParamFields,
  getActivityOfB,
  getActivityOfS,
  createActivityOfS,
  updateActivityOfS,
  getActivityOfSByParentIDAndSpokesmanID,
  pauseActivityOfS,
  resumeActivityOfS,
  publishActivityOfS
} from "../../service/activityService";
import { getBrandSpokesmanInfoSafely } from "../../service/brandSpokesmanService"
import { isUndefined } from "mmb";
const router = createSpokesmanApiMWRouter();

 // 获取代言的活动详情(代言人管理视角)
router.get("/spn/:spokesmanId/activity/:activityId", async (ctx: AppCTX, next) => {
  const { spokesmanId, activityId } = ctx.params;
  const { userId } = ctx;

  const activity = await getActivityOfS(activityId);

  // 代言人自己发布的活动
  if (activity.spokesmanId !== spokesmanId) {
    ctx.throw(400, "no belong current spokesman");
  }

  ctx.body = {
    ...activity,
  };
});

 // 获取品牌活动详情(代言人查看品牌活动视角)
 router.get("/spn/:spokesmanId/brandActivity/:activityId", async (ctx: AppCTX, next) => {
  const { spokesmanId, activityId } = ctx.params;
  const { userId } = ctx;

  // todo, 活动的品牌必须是代言人id对应的品牌
  
  const activity = await getActivityOfB(activityId);
  const brandSpokesmanInfo = await getBrandSpokesmanInfoSafely(spokesmanId)
  if (activity.brandId !== brandSpokesmanInfo.brandId) {
    ctx.throw(400, "mismatch current spokesman's brandId ");
  }

  ctx.body = {
    ...activity,
  };
});

// 获取活动的购买记录列表(代言人管理视角)
router.get("/spn/:spokesmanId/activity/:activityId/buyRecords", async (ctx: AppCTX, next) => {
  const { spokesmanId, activityId } = ctx.params;
  const { userId } = ctx;
  ctx.body = {
    spokesmanId,
    activityId,
    userId,
    buyRecords: [
      {
        orderId: 5123,
        ordered: Date.now(),
        items: [
          {
            itemInfo: { productName: "商品名称1", keyA: "a2", keyB: "b1" },
            quality: 1
          },
        ],
      },
      {
        orderId: 5124,
        ordered: Date.now(),
        items: [
          {
            itemInfo: { productName: "商品名称1", keyA: "a1", keyB: "b1" },
            quality: 1
          },
        ],
      },
    ],
  };
}
);

// 新增代言活动
router.post("/spn/:spokesmanId/activity", async (ctx: AppCTX, next) => {
  const { spokesmanId } = ctx.params;
  const {
    endorseActivityId,
    name,
    intro,
    background,
    tel,
    activityProductIsShows,
  } = ctx.request.body;
  const { userId } = ctx;

  // 1、需要独立验证spokesmanId是针对endorseActivityId活动的品牌代言人
  const endorseActivity = await getActivityOfB(endorseActivityId);
  const brandSpokesmanInfo = await getBrandSpokesmanInfoSafely(spokesmanId)
  if (endorseActivity.brandId !== brandSpokesmanInfo.brandId) {
    ctx.throw(400, "mismatch current spokesman's brandId ");
  }

  // todo 2、需要独立验证spokesmanId是针对endorseActivity已经代言过活动
  const activityEndorseBefore = await getActivityOfSByParentIDAndSpokesmanID({ 
    parentId: endorseActivityId,
    spokesmanId
  })
  if (!isUndefined(activityEndorseBefore)) {
    ctx.throw(400, "already endorsed");
  }

  checkActivityOfSParamFields({
    endorseActivityId,
    activityProductIsShows,
  });

  const activity = await createActivityOfS({
    parentId: endorseActivityId,
    spokesmanId,
    pubUserId: userId,
    name,
    intro,
    background,
    tel,
    // product is_shows
    activityProductIsShows,
  });

  ctx.body = {
    ...activity,
  };
});

// 更新代言活动
router.put("/spn/:spokesmanId/activity/:activityId", async (ctx: AppCTX, next) => {
  const { spokesmanId, activityId } = ctx.params;
  const {
    name,
    intro,
    background,
    tel,
    activityProductIsShows, // type checker
  } = ctx.request.body;
  const { userId } = ctx;

  // todo 需要独立验证spokesmanId===activityId活动的spokesmanId
  

  // 修改商品隐藏显示,修改描述
  checkActivityOfSParamFields({
    activityProductIsShows,
  }, false);

  const activity = await updateActivityOfS({
    activityId,
    name,
    intro,
    background,
    tel,
    activityProductIsShows,
  });

  ctx.body = {
    ...activity,
  };
});

// 暂停代言活动S
router.put("/spn/:spokesmanId/activity/:activityId/pause", async (ctx: AppCTX, next) => {
  const { spokesmanId, activityId } = ctx.params;
  const { userId } = ctx;

  const processStatus = await pauseActivityOfS({activityId, userId});
    ctx.body = {
      activityId,
      processStatus,
    };
});

// 恢复代言活动S
router.put("/spn/:spokesmanId/activity/:activityId/resume", async (ctx: AppCTX, next) => {
  const { spokesmanId, activityId } = ctx.params;
  const { userId } = ctx;

  const processStatus = await resumeActivityOfS({activityId, userId});
    ctx.body = {
      activityId,
      processStatus,
    };
});

// 发布品牌活动
router.put("/spn/:spokesmanId/activity/:activityId/publish", async (ctx: AppCTX, next) => {
  const { spokesmanId, activityId } = ctx.params;
  const { userId } = ctx;

  const processStatus = await publishActivityOfS({activityId, userId});
    ctx.body = {
      activityId,
      processStatus,
    };
});

export default router;
